package com.oauth.auth.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gwtwidgets.server.spring.GWTSpringController;
import org.gwtwidgets.server.spring.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.oauth.auth.client.beans.MemberClientBean;
import com.oauth.auth.client.rpc.MemberRemoteService;
import com.oauth.auth.shared.AuthUtil;
import com.oauth.service.members.beans.Member;
import com.oauth.service.members.service.MemberService;

public class MemberController extends GWTSpringController implements MemberRemoteService {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7543496543613271812L;

	@Autowired
	private MemberService mMemberService;

	public MemberClientBean getMember() throws Exception {
		HttpServletRequest request = ServletUtils.getRequest();
		HttpServletResponse response = ServletUtils.getResponse();
		UserService userService = UserServiceFactory.getUserService();
		if (userService.isUserLoggedIn() == false) {
			AuthUtil.writeResponse(request, response);
			return null;
		}
		User user = userService.getCurrentUser();
		Member member = mMemberService.getMember(user.getEmail(), true);
		member.setNickName(user.getNickname());
		return AuthUtil.getMember(member);
	}

	public void setMemberService(MemberService pMemberService) {
		mMemberService = pMemberService;
	}
}
